@echo off
chcp 65001
setlocal


set ELASTICSEARCH_URL=http://localhost:9200
set ELASTIC_USERNAME=elastic
set ELASTIC_PASSWORD=123456
set NEW_USERNAME=kibanauser
set NEW_PASSWORD=123456
set NEW_ROLE=monitoring_role

:: 删除已存在的用户（如果存在）
curl -X DELETE "%ELASTICSEARCH_URL%/_security/user/%NEW_USERNAME%" -u %ELASTIC_USERNAME%:%ELASTIC_PASSWORD%
echo 用户（如果存在）已删除。

:: 删除已存在的角色（如果存在）
curl -X DELETE "%ELASTICSEARCH_URL%/_security/role/%NEW_ROLE%" -u %ELASTIC_USERNAME%:%ELASTIC_PASSWORD%
echo 角色（如果存在）已删除。

:: 创建一个新的角色
curl -X POST "%ELASTICSEARCH_URL%/_security/role/%NEW_ROLE%" -H "Content-Type: application/json" -d "{""cluster"": [""all""], ""indices"": [{""names"": [""*""], ""privileges"": [""all""]}], ""applications"": [], ""run_as"": [], ""metadata"": {}, ""transient_metadata"": {""enabled"": true}}" -u %ELASTIC_USERNAME%:%ELASTIC_PASSWORD%
echo 新角色创建完成。

:: 创建用户并分配角色
curl -X POST "%ELASTICSEARCH_URL%/_security/user/%NEW_USERNAME%" -H "Content-Type: application/json" -d "{""password"" : ""%NEW_PASSWORD%"", ""roles"" : [""%NEW_ROLE%""], ""full_name"" : ""New User"", ""email"" : ""newuser@example.com"", ""metadata"" : { ""intelligence"" : 7 }}" -u %ELASTIC_USERNAME%:%ELASTIC_PASSWORD%
echo 用户创建和角色分配完成。

pause
endlocal